<?php

namespace App\Http\Controllers\Admin\Framework\Common;

use App\Http\Controllers\Admin\ParentController;
use App\Master\Framework\Extend\DataUtil;
use App\Master\Framework\Library\Uploads;
use App\Models\Admin\Framework\AdminGalleryGroupModel;
use App\Models\Admin\Framework\AdminGalleryModel;
use App\Models\Admin\Framework\AdminUsersModel;
use App\Utils\AppResult;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class CenterController extends ParentController
{

    public function __construct(array $attributes = [])
    {
        parent::__construct();
    }

    /**
     * 获取用户信息
     * @return string
     */
    public function info()
    {
        $user_id = auth('admin')->id();
        $info = AdminUsersModel::query()->where('id',$user_id)->select(['id','name','account'])->first();
        if (!$info){
            return AppResult::response201('操作超时，请刷新页面重试！');
        }
        return AppResult::response200('操作成功！',$info);
    }

    /**
     * 编辑
     * @param Request $request
     * @return string
     */
    public function update(Request $request)
    {
        $params = $request->all();
        if (empty($params['id'])) {
            return AppResult::response203('操作超时！');
        }
        if (empty($params['name'])) {
            return AppResult::response203('请填写名称！');
        }
        $user_id = auth('admin')->id();
        if ($user_id != $params['id']){
            return AppResult::response203('操作超时！');
        }

        // 初始化空数据
        $form = DataUtil::field($params, ['password', 'status', 'is_system']);
        if (!empty($params['password'])) {
            $form['password'] = Hash::make(md5($params['password']));
        }
        $form['updated_at'] = time();
        if (!AdminUsersModel::query()->where('id', $params['id'])->update($form)) {
            return AppResult::response201('操作失败');
        }
        return AppResult::response200('操作成功');
    }
}